﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FWMS.COMMAND;

namespace FWMS.DAO
{
    class ChukumingxiDao
    {
        //原材料查询全部
        public Bean.Chukuguanli_yuancailiaoBean chukumingxi_yuancailiaoDaoSelect(DateTime dateStart, DateTime dateEnd, int tabIndex)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            string sql = "";

            sql = "select chuku_pinming, chuku_guige,chuku_beizhu,chuku_danwei,sum(chuku_zhongliang),sum(chuku_jianshu)  from chukuguanli where chuku_yanshou between '" + dateStart.ToString("yyyyMMdd") + "' and '" + dateEnd.ToString("yyyyMMdd") + "' and chuku_type = '" + tabIndex + "' group by chuku_pinming,chuku_guige,chuku_beizhu,chuku_danwei  order by chuku_pinming,chuku_guige";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }

        //半成品查询
        public Bean.Chukuguanli_yuancailiaoBean chukumingxi_chengpinDaoSelect(DateTime dateStart, DateTime dateEnd, int tabIndex)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            string sql = "";

            sql = "select chuku_pinming, chuku_guige,chuku_beizhu,chuku_danwei,sum(chuku_zhongliang),sum(chuku_jianshu)  from chukuguanli where (chuku_type='3' or chuku_type = '4') and  chuku_yanshou between '" + dateStart.ToString("yyyyMMdd") + "' and '" + dateEnd.ToString("yyyyMMdd") + "' and chuku_beizhu like '%用%' group by chuku_pinming,chuku_guige,chuku_beizhu,chuku_danwei  order by chuku_pinming,chuku_guige";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }


        //实际使用重量查询
        public string chukushijiZhongliang(string dateStart, string dateEnd, string pinming, string guige, string beizhu, string type)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            string sql = "";
            string shijizhongliang = "";
            if ("2".Equals(type.ToString()))
            {
                //chuku_jianshu
                sql = "select chuku_jianshu from chukumingxijiji where start_date = '" + dateStart + "' and end_date = '" + dateEnd + "' and pinming='" + pinming + "' and guige='" + guige + "' and beizhu = '" + beizhu + "' and type='" + type + "'";
            }
            else
            {
                //chuku_jianshu
                sql = "select shijishiyongzhongliang from chukumingxijiji where start_date = '" + dateStart + "' and end_date = '" + dateEnd + "' and pinming='" + pinming + "' and guige='" + guige + "' and beizhu = '" + beizhu + "' and type='" + type + "'";
            }


            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    shijizhongliang = dbbase.rows[0, 0].ToString();
                }
            }
            return shijizhongliang;


        }
        //成品查询
        public Bean.Chukuguanli_yuancailiaoBean chukumingxi_realchengpinDaoSelect(DateTime dateStart, DateTime dateEnd, int tabIndex)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            string sql = "";

            sql = "select chuku_pinming, chuku_guige,chuku_beizhu,chuku_danwei,sum(chuku_zhongliang) ,sum(chuku_jianshu) from chukuguanli where (chuku_type='3' or chuku_type = '4') and  chuku_yanshou between '" + dateStart.ToString("yyyyMMdd") + "' and '" + dateEnd.ToString("yyyyMMdd") + "' and chuku_beizhu like '%LFC%' group by chuku_pinming, chuku_guige,chuku_beizhu,chuku_danwei order by chuku_pinming,chuku_guige";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }


        //原材料查询全部
        public Bean.Chukuguanli_tiaoweiliaoBean chukumingxi_tiaoweiliaoDaoSelect(DateTime dateStart, DateTime dateEnd)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_tiaoweiliaoBean bean = new FWMS.Bean.Chukuguanli_tiaoweiliaoBean();
            string sql = "";
            sql = "select chuku_pinming, chuku_pinming,chuku_type,sum(chuku_zhongliang)  from chukuguanli where chuku_yanshou between '" + dateStart.ToString("yyyyMMdd") + "' and '" + dateEnd.ToString("yyyyMMdd") + "' and chuku_type = '1' group by chuku_pinming,chuku_type  order by chuku_pinming";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }


        //库存表更新
        public bool chukumingxi_yuancailiaoDaoUpdate(Bean.Chukuguanli_yuancailiaoBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();

            string delSql = "delete from chukumingxijiji where type = '0' and start_date='" + bean.Selectresult[0, 6].ToString() + "' and end_date = '" + bean.Selectresult[0, 7].ToString() + "'";

            if (!dbbase.ExecuteSQL(delSql.Trim()))
            {
                return false;
            }

            for (int i = 0; i < bean.Resultrow; i++)
            {

                string inserSql = "insert into chukumingxijiji(pinming,guige,beizhu,danwei,shijichukuzhongliang,shijishiyongzhongliang,start_date,end_date,type,save_date,chuku_jianshu) values(";
                inserSql += "'" + bean.Selectresult[i, 0].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 1].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 2].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 3].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 4].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 5].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 6].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 7].ToString() + "', ";
                inserSql += "'0',";
                inserSql += "'" + System.DateTime.Now.ToString("yyyyMMdd") + "', ";
                inserSql += "'" + bean.Selectresult[i, 8].ToString() + "') ";
                if (!dbbase.ExecuteSQL(inserSql.Trim()))
                {
                    return false;
                }
            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //调料库存表更新
        public bool chukumingxi_tiaoweiliaoDaoUpdate(Bean.Chukuguanli_tiaoweiliaoBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();

            string delSql = "delete from chukumingxijiji where type = '1' and start_date='" + bean.Selectresult[0, 6].ToString() + "' and end_date = '" + bean.Selectresult[0, 7].ToString() + "'";


            if (!dbbase.ExecuteSQL(delSql.Trim()))
            {
                return false;
            }

            for (int i = 0; i < bean.Resultrow; i++)
            {

                string inserSql = "insert into chukumingxijiji(pinming,guige,beizhu,danwei,shijichukuzhongliang,shijishiyongzhongliang,start_date,end_date,type,save_date,chuku_jianshu) values(";
                inserSql += "'" + bean.Selectresult[i, 0].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 1].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 2].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 3].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 4].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 5].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 6].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 7].ToString() + "', ";
                inserSql += "'1',";
                inserSql += "'" + System.DateTime.Now.ToString("yyyyMMdd") + "', ";
                inserSql += "'" + bean.Selectresult[i, 8].ToString() + "') ";
                if (!dbbase.ExecuteSQL(inserSql.Trim()))
                {
                    return false;
                }
            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        //出库管理表更新
        public bool chukuguanli_yuancailiaoDaoUpdate(Bean.Chukuguanli_yuancailiaoBean bean, string startDay, string endDay)
        {
            Bean.Chukuguanli_yuancailiaoBean tempBean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";

            /**     //计算比例用选择阶段的总重量
                 string zhongliangInPeroid_sql = "select chuku_zhongliang from chukuguanli where pinming = '" + bean.Selectresult[i, 1] + "' and chuku_zhiban = '" + bean.Selectresult[i, 0] + "'";

                 float zhongliangInPeroid = 0;

                 if (dbbase.ExecuteSQL(zhongliang_sql.Trim()))
                 {
                     zhongliangInPeroid = float.Parse(dbbase.rows[0, 0].ToString());

                 }
             * **/
            string zhongliang_sql = "";
            string update_sql = "";

            for (int i = 0; i < bean.Resultrow; i++)
            {
                //出库表单重
                if (bean.Selectresult[i, 1] == null || "".Equals(bean.Selectresult[i, 1]))
                {
                    zhongliang_sql = "select chuku_no,chuku_zhongliang from chukuguanli where chuku_type = '0' and chuku_pinming = '" + bean.Selectresult[i, 0] + "' and (chuku_guige='' or chuku_guige IS NULL) and chuku_yanshou between '" + startDay + "' and '" + endDay + "'";
                }
                else
                {
                    zhongliang_sql = "select chuku_no,chuku_zhongliang from chukuguanli where chuku_type = '0' and chuku_pinming = '" + bean.Selectresult[i, 0] + "' and chuku_guige='" + bean.Selectresult[i, 1] + "' and chuku_yanshou between '" + startDay + "' and '" + endDay + "'";
                }


                float chuku_zhongliang = 0;
                string chuku_no = "";
                double plusValue = 0;
                double lastValue = 0;
                int currentRow = 0;
                if (dbbase.ExecuteSQL(zhongliang_sql.Trim()))
                {
                    for (int j = 0; j < dbbase.Row_Number; j++)
                    {
                        chuku_no = dbbase.rows[j, 0].ToString();
                        chuku_zhongliang = float.Parse(dbbase.rows[j, 1].ToString());
                        currentRow = dbbase.Row_Number - 1;
                        if (j == currentRow && j != 0)
                        {
                            lastValue = Math.Round((double.Parse(bean.Selectresult[i, 3].ToString()) - plusValue), 3);
                            if (bean.Selectresult[i, 1] == null || "".Equals(bean.Selectresult[i, 1]))
                            {
                                update_sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round((chuku_zhongliang - lastValue), 3)) + "' where chuku_no = '" + chuku_no + "' and chuku_type='0' and chuku_pinming='" + bean.Selectresult[i, 0] + "' and (chuku_guige='' or chuku_guige IS NULL)";
                            }
                            else
                            {
                                update_sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round((chuku_zhongliang - lastValue), 3)) + "' where chuku_no = '" + chuku_no + "' and chuku_type='0' and chuku_pinming='" + bean.Selectresult[i, 0] + "' and chuku_guige='" + bean.Selectresult[i, 1] + "'";
                            }
                        }
                        else
                        {
                            if (bean.Selectresult[i, 1] == null || "".Equals(bean.Selectresult[i, 1]))
                            {
                                update_sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round(chuku_zhongliang - (Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()), 3)), 3)) + "' where chuku_no = '" + chuku_no + "' and chuku_type='0' and chuku_pinming='" + bean.Selectresult[i, 0] + "' and (chuku_guige='' or chuku_guige IS NULL)";
                            }
                            else
                            {
                                update_sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round(chuku_zhongliang - (Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()), 3)), 3)) + "' where chuku_no = '" + chuku_no + "' and chuku_type='0' and chuku_pinming='" + bean.Selectresult[i, 0] + "' and chuku_guige='" + bean.Selectresult[i, 1] + "'";
                            }

                        }

                        if (!dbbase.ExecuteSQL(update_sql.Trim()))
                        {
                            return false;
                        }
                        plusValue += Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()), 3);


                    }



                }


            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //调味料出库管理表更新
        public bool chukuguanli_tiaoWeiliaoDaoUpdate(Bean.Chukuguanli_tiaoweiliaoBean bean, string startDay, string endDay)
        {
            Bean.Chukuguanli_tiaoweiliaoBean tempBean = new FWMS.Bean.Chukuguanli_tiaoweiliaoBean();
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";

            /**     //计算比例用选择阶段的总重量
                 string zhongliangInPeroid_sql = "select chuku_zhongliang from chukuguanli where pinming = '" + bean.Selectresult[i, 1] + "' and chuku_zhiban = '" + bean.Selectresult[i, 0] + "'";

                 float zhongliangInPeroid = 0;

                 if (dbbase.ExecuteSQL(zhongliang_sql.Trim()))
                 {
                     zhongliangInPeroid = float.Parse(dbbase.rows[0, 0].ToString());

                 }
             * **/

            for (int i = 0; i < bean.Resultrow; i++)
            {
                //出库表单重
                string zhongliang_sql = "select chuku_no,chuku_zhongliang from chukuguanli where chuku_type = '1' and chuku_pinming = '" + bean.Selectresult[i, 0] + "' and chuku_yanshou between '" + startDay + "' and '" + endDay + "'";

                float chuku_zhongliang = 0;
                string chuku_no = "";
                double plusValue = 0;
                double lastValue = 0;
                int currentRow = 0;
                if (dbbase.ExecuteSQL(zhongliang_sql.Trim()))
                {
                    for (int j = 0; j < dbbase.Row_Number; j++)
                    {
                        chuku_no = dbbase.rows[j, 0].ToString();
                        chuku_zhongliang = float.Parse(dbbase.rows[j, 1].ToString());
                        currentRow = dbbase.Row_Number - 1;
                        if (j == currentRow && j != 0)
                        {
                            lastValue = Math.Round((double.Parse(bean.Selectresult[i, 3].ToString()) - plusValue), 3);
                            sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round((chuku_zhongliang - lastValue), 3)) + "' where chuku_no = '" + chuku_no + "'";
                        }
                        else
                        {
                            sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round(chuku_zhongliang - (Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()), 3)), 3)) + "' where chuku_no = '" + chuku_no + "'";
                        }

                        if (!dbbase.ExecuteSQL(sql.Trim()))
                        {
                            return false;
                        }
                        plusValue += Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()), 3);


                    }



                }


            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //辅料库存表更新
        public bool chukumingxi_fuliaoDaoUpdate(Bean.Chukuguanli_fuliaoBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();

            string delSql = "delete from chukumingxijiji where type = '2' and start_date='" + bean.Selectresult[0, 6].ToString() + "' and end_date = '" + bean.Selectresult[0, 7].ToString() + "'";


            if (!dbbase.ExecuteSQL(delSql.Trim()))
            {
                return false;
            }

            for (int i = 0; i < bean.Resultrow; i++)
            {

                string inserSql = "insert into chukumingxijiji(pinming,guige,beizhu,danwei,shijichukuzhongliang,shijishiyongzhongliang,start_date,end_date,type,save_date,chuku_jianshu) values(";
                inserSql += "'" + bean.Selectresult[i, 0].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 1].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 2].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 3].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 4].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 5].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 6].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 7].ToString() + "', ";
                inserSql += "'2',";
                inserSql += "'" + System.DateTime.Now.ToString("yyyyMMdd") + "', ";
                inserSql += "'" + bean.Selectresult[i, 8].ToString() + "') ";
                if (!dbbase.ExecuteSQL(inserSql.Trim()))
                {
                    return false;
                }
            }


            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        //半成品库存表更新
        public bool chukumingxi_chengpinDaoUpdate(Bean.Chukuguanli_chanpinBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();

            string delSql = "delete from chukumingxijiji where type = '3' and start_date='" + bean.Selectresult[0, 6].ToString() + "' and end_date = '" + bean.Selectresult[0, 7].ToString() + "'";

            if (!dbbase.ExecuteSQL(delSql.Trim()))
            {
                return false;
            }

            for (int i = 0; i < bean.Resultrow; i++)
            {

                string inserSql = "insert into chukumingxijiji(pinming,guige,beizhu,danwei,shijichukuzhongliang,shijishiyongzhongliang,start_date,end_date,type,save_date,chuku_jianshu) values(";
                inserSql += "'" + bean.Selectresult[i, 0].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 1].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 2].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 3].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 4].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 5].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 6].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 7].ToString() + "', ";
                inserSql += "'3',";
                inserSql += "'" + System.DateTime.Now.ToString("yyyyMMdd") + "', ";
                inserSql += "'" + bean.Selectresult[i, 8].ToString() + "') ";
                if (!dbbase.ExecuteSQL(inserSql.Trim()))
                {
                    return false;
                }
            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //成品库存表更新
        public bool chukumingxi_realchengpinServicUpdate(Bean.Chukuguanli_chanpinBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();

            string delSql = "delete from chukumingxijiji where type = '3' and save_date='" + System.DateTime.Now.ToString("yyyyMMdd") + "'";

            if (!dbbase.ExecuteSQL(delSql.Trim()))
            {
                return false;
            }

            for (int i = 0; i < bean.Resultrow; i++)
            {

                string inserSql = "insert into chukumingxijiji(pinming,guige,beizhu,danwei,shijichukuzhongliang,shijishiyongzhongliang,start_date,end_date,type,save_date,chuku_jianshu) values(";
                inserSql += "'" + bean.Selectresult[i, 0].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 1].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 2].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 3].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 4].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 5].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 6].ToString() + "', ";
                inserSql += "'" + bean.Selectresult[i, 7].ToString() + "', ";
                inserSql += "'3',";
                inserSql += "'" + System.DateTime.Now.ToString("yyyyMMdd") + "', ";
                inserSql += "'" + bean.Selectresult[i, 8].ToString() + "') ";
                if (!dbbase.ExecuteSQL(inserSql.Trim()))
                {
                    return false;
                }
            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        public bool kuchunguanli_Update(System.Windows.Forms.DataGridView gridview,string type)
        {
            DB.DBbase dbbase = new DB.DBbase();
            foreach (System.Windows.Forms.DataGridViewRow row in gridview.Rows)
            {
                if (row.Cells[0].Value == null)
                {
                    string pinming = Command.nullToObject(gridview.Rows[row.Index - 1].Cells[0].Value).ToString();
                    string guige = Command.nullToObject(gridview.Rows[row.Index - 1].Cells[1].Value).ToString();
                    string shengyu = Command.nullToObject(row.Cells[7].Value).ToString();
                    string chukuzhongliang = Command.nullToObject(row.Cells[6].Value).ToString();
                    //库存表中取出现有该规格的重量
                    string selectSql = "";
                    string updateSql = "";
                    if (guige == null || "".Equals(guige))
                    {
                        selectSql = "select thismonthzhongliang from kucunguanli where pinming = '" + pinming + "' and (guige='' or guige is null) and cailiao_type='" + type + "'";

                    }
                    else
                    {
                        selectSql = "select thismonthzhongliang from kucunguanli where pinming = '" + pinming + "' and guige='" + guige + "' and cailiao_type='" + type + "'";
                    }
                    if (dbbase.ExecuteSQL(selectSql.Trim()))
                    {
                        string zhongliang = dbbase.rows[0, 0].ToString();
                        double afterZhongliang = Math.Round((float.Parse(zhongliang) + float.Parse(chukuzhongliang) - float.Parse(shengyu)), 3);
                        if (guige == null || "".Equals(guige))
                        {
                            updateSql = "update kucunguanli set thismonthzhongliang= '" + Convert.ToString(afterZhongliang) + "'  where pinming = '" + pinming + "' and (guige='' or guige is null) and cailiao_type='" + type + "'";
                        }
                        else
                        {
                            updateSql = "update kucunguanli set thismonthzhongliang= '" + Convert.ToString(afterZhongliang) + "'  where pinming = '" + pinming + "' and guige='" + guige + "' and cailiao_type='" + type + "'";
                        }
                        if (!dbbase.ExecuteSQL(updateSql.Trim()))
                        {
                            return false;
                        }
                    }
                }
            }
            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //按照时间段查询
        public Bean.Chukumingxi_yuancailiaoBean mingxi_ServiceSelectByPeriod(DateTime dateStart, DateTime dateEnd, string type)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukumingxi_yuancailiaoBean bean = new FWMS.Bean.Chukumingxi_yuancailiaoBean();
            string sql = "";

            sql = "select distinct save_date,type, start_date,end_date from chukumingxijiji where start_date between '" + dateStart.ToString("yyyyMMdd") + "' and  '" + dateEnd.ToString("yyyyMMdd") + "' and type = '" + type + "'  order by save_date";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }

        //按照时间段查询
        public Bean.Chukuguanli_yuancailiaoBean mingxi_ServiceSelectByJijiDate(string startDay, string endDay, string type)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            string sql = "";

            sql = "select pinming, guige,beizhu,danwei,shijichukuzhongliang,shijishiyongzhongliang,chuku_jianshu from chukumingxijiji where start_date = '" + startDay + "' and end_date='" + endDay + "' and type = '" + type + "'  order by pinming,guige,beizhu";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }

        //明细最后集计日期
        public string mingxiJiJiLastDate(string type)
        {
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";

            sql = "select max(end_date)  from chukumingxijiji where  type = '" + type + "'";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    return dbbase.rows[0, 0].ToString();
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }

        //明细最后集计开始日期
        public string mingxiJiJiLastDateStart(string type)
        {
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";

            sql = "select max(start_date)  from chukumingxijiji where  type = '" + type + "'";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    return dbbase.rows[0, 0].ToString();
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }


    }

}
